package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.StockStrategyMapper;
import com.canaan.business.domain.basic.StockStrategy;
import com.canaan.business.service.basic.IStockStrategyService;
import com.canaan.common.annotation.TableName;
/**
 * 库存策略Service业务层处理
 *
 * @author canaan
 * @date 2025-04-15
 */
@Service
public class StockStrategyServiceImpl implements IStockStrategyService {
    @Resource
    private StockStrategyMapper stockStrategyMapper;

    /**
     * 查询库存策略
     *
     * @param id 库存策略主键
     * @return 库存策略
     */
    @Override
    public StockStrategy selectStockStrategyById(Long id) {
        return stockStrategyMapper.selectStockStrategyById(id);
    }

    /**
     * 选择入库默认策略
     * @return
     */
    @Override
    public StockStrategy selectInStockDefaultStrategy(int strategyType){
        return stockStrategyMapper.selectInStockDefaultStrategy(strategyType);
    }

    /**
     * 选择出库默认策略
     * @return
     */
    @Override
    public StockStrategy selectOutStockDefaultStrategy(){
        return  stockStrategyMapper.selectOutStockDefaultStrategy();
    }

    /**
     * 查询库存策略列表
     *
     * @param stockStrategy 库存策略
     * @return 库存策略
     */
    @Override
    public List<StockStrategy> selectStockStrategyList(StockStrategy stockStrategy) {
        return stockStrategyMapper.selectStockStrategyList(stockStrategy);
    }

    /**
     * 新增库存策略
     *
     * @param stockStrategy 库存策略
     * @return 结果
     */
    @Override
    public int insertStockStrategy(StockStrategy stockStrategy) {
        if(StringUtils.isBlank(stockStrategy.getNo())){
            stockStrategy.setNo(TableNoUtil.getNo(StockStrategy.class));
        }
        stockStrategy.setCreateBy(SecurityUtils.getUsername());
        stockStrategy.setCreateTime(DateUtils.getNowDate());
        return stockStrategyMapper.insertStockStrategy(stockStrategy);
    }

    /**
     * 批量新增库存策略
     *
     * @param stockStrategyList 库存策略列表
     * @return 结果
     */
    @Override
    public int batchInsertStockStrategy(List<StockStrategy> stockStrategyList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockStrategy stockStrategy : stockStrategyList){
            if(StringUtils.isBlank(stockStrategy.getNo())){
                stockStrategy.setNo(TableNoUtil.getNo(StockStrategy.class));
            }
            stockStrategy.setCreateBy(username);
            stockStrategy.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockStrategyList.size();) {
            int endIndex = i+500;
            if (endIndex > stockStrategyList.size()) {
                endIndex = stockStrategyList.size();
            }
            insertNum = insertNum + stockStrategyMapper.batchInsertStockStrategy(stockStrategyList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存策略
     *
     * @param stockStrategy 库存策略
     * @return 结果
     */
    @Override
    public int updateStockStrategy(StockStrategy stockStrategy) {
        stockStrategy.setUpdateBy(SecurityUtils.getUsername());
        stockStrategy.setUpdateTime(DateUtils.getNowDate());
        return stockStrategyMapper.updateStockStrategy(stockStrategy);
    }

    /**
     * 批量修改库存策略
     *
     * @param stockStrategyList 库存策略列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockStrategy(List<StockStrategy> stockStrategyList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockStrategy stockStrategy : stockStrategyList){
            stockStrategy.setUpdateBy(username);
            stockStrategy.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockStrategyList.size();) {
            int endIndex = i+500;
            if (endIndex > stockStrategyList.size()) {
                endIndex = stockStrategyList.size();
            }
            updateNum = updateNum + stockStrategyMapper.batchUpdateStockStrategy(stockStrategyList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存策略
     *
     * @param ids 需要删除的库存策略主键集合
     * @return 结果
     */
    @Override
    public int deleteStockStrategyByIds(Long[] ids) {
        return stockStrategyMapper.deleteStockStrategyByIds(ids);
    }

    /**
     * 删除库存策略信息
     *
     * @param id 库存策略主键
     * @return 结果
     */
    @Override
    public int deleteStockStrategyById(Long id) {
        return stockStrategyMapper.deleteStockStrategyById(id);
    }

    /**
     * 查询库存策略
     *
     * @param no 编号
     * @return 库存策略
     */
    @Override
    public StockStrategy selectStockStrategyByNo(String no) {
        return stockStrategyMapper.selectStockStrategyByNo(no);
    }

    /**
     * 批量删除库存策略
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockStrategyByNos(String[] nos) {
        return stockStrategyMapper.deleteStockStrategyByNos(nos);
    }

    /**
     * 删除库存策略信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockStrategyByNo(String no) {
        return stockStrategyMapper.deleteStockStrategyByNo(no);
    }
}
